fn main() {
    println!("Hello, world!");

    let res = Solution2::length_of_longest_substring(String::from("abacd"));
    println!("{}", res);
}

struct Solution;

impl Solution {
    pub fn length_of_longest_substring(s: String) -> i32 {
        // abacd
        let mut max = 0;
        let mut vec = vec![];
        for ele in s.chars() {
            vec.push(ele);
            if Solution::check(&vec) {
                if vec.len() > max {
                    max = vec.len();
                }
            } else {
                vec.remove(0);
            }
        }

        max as i32
    }

    fn check(vec: &Vec<char>) -> bool {
        let mut tmp = vec![];
        for ele in vec {
            if !tmp.contains(&ele) {
                tmp.push(ele);
            }
        }
        tmp.len() == vec.len()
    }
}

struct Solution2;

impl Solution2 {
    pub fn length_of_longest_substring(s: String) -> i32 {
        // abacd
        let length = s.len();
        let mut max = 0;
        let mut left_index: usize = 0;
        for c1 in s.chars() {
            let mut right_index = left_index + 1;
            if max > length - right_index {
                break;
            }
            let mut rec = [0_u8; 128];
            rec[c1 as usize] = 1;
            let sub_str = &s[right_index..length];
            for ele in sub_str.chars() {
                if rec[ele as usize] == 1 {
                    break;
                } else {
                    rec[ele as usize] = 1;
                }
                right_index = right_index + 1;
            }
            let len = right_index - left_index;
            if max < len {
                max = len;
            }
            left_index = left_index + 1;
        }
        max as i32
    }
}
